//
//  CYMyApplyRewardVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/2/23.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYMyApplyRewardVC.h"
#import "CYMyApplyCell.h"
#import "UITableViewCell+Category.h"
#import "MBProgressHUD+LE.h"
#import "CYDiscoveryHttpTool.h"
#import "CYCompanyUserTool.h"
#import "CYSimpleLoanM.h"
#import "CYCreditTool.h"
#import "CYCarAppealVC.h"
#import "LEPresentingAnimatorTool.h"
#import "CYLoginHttpTool.h"

@interface CYMyApplyRewardVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, weak) UITableView *tableview;

@property(nonatomic, strong) NSArray *simpleList;
@end

@implementation CYMyApplyRewardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTable];
    [self loadData];
    self.title = @"我申请的悬赏";
}

- (void)setupTable {
    UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    self.tableview = table;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

- (void)loadData {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYDiscoveryHttpTool getOwnRewoardWithParam:@{@"userId": user.userId, @"token": user.token, @"type": @"0", @"page": @"0"} success:^(NSArray *simpleList) {
            [MBProgressHUD hideHUDForView:self.view];
            self.simpleList = simpleList;
            [self.tableview reloadData];
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showError:message];
            if (!isLogin) {
                [self.navigationController popToRootViewControllerAnimated:NO];
                [CYCompanyUserTool logoutWithController:self];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.simpleList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CYSimpleLoanM *model = self.simpleList[indexPath.row];
    CYMyApplyCell *cell = [CYMyApplyCell cellWithTableView:tableView];
    cell.licencePlateLabel.text = model.carNumberH;
    cell.moneyLabel.text = model.rewardMoney;
    cell.companyName.text = model.companyNameH;
    cell.companyPhone.text = model.companyPhoneH;
    cell.timeLabel.text = [CYCreditTool timewithLongLongValue:model.limitTime.longLongValue formatter:@"yyyy-MM-dd"];
    cell.name.text = model.nameH;
    cell.phone.text = model.phoneH;
    cell.idCard.text = model.certificateNumberH;
    cell.tipLabel.text = model.statusApplyStr;
    cell.applyName.text = model.linkName;
    cell.applyPhone.text = model.linkPhone;
    cell.memo.text = model.linkMemo;
    if (model.status.intValue == 2) {
        cell.appealBtn.hidden = NO;
        cell.tipConstraint.constant += 34;
        cell.appealBtn.tag = indexPath.row;
        [cell.appealBtn addTarget:self action:@selector(appealClick:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        cell.appealBtn.hidden = YES;
        cell.tipConstraint.constant = 16;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}
/*!
 *  @brief  申诉按钮点击事件
 */
- (void)appealClick:(UIButton *)btn {
    CYSimpleLoanM *model = self.simpleList[btn.tag];
    CYCarAppealVC *to = [[CYCarAppealVC alloc] init];
    to.model = model;
    [LEPresentingAnimatorTool presentingAnimatorWithController:to showIn:self completion:nil];
    CYWeakObject(wself, self);
    [to appealSuccessed:^{
        [wself loadData];
        //        [wself.navigationController popViewControllerAnimated:YES];
    }];
}
@end
